import de.motsangria.enums.ClubFunction

class BootStrap {

  def authenticateService

  def init = {servletContext ->
    String md5pass = authenticateService.passwordEncoder('motsangria_login')

    //Generate default roles
    def roleUser = Role.findByAuthority(Role.USER)
    if (!roleUser) roleUser = new Role(description: 'Regular user', authority: Role.USER).save()
    def roleAdmin = Role.findByAuthority(Role.ADMIN)
    if (!roleAdmin) roleAdmin = new Role(description: 'Administrator', authority: Role.ADMIN).save()

    //Generate default users
    def userAlex = User.findByUsername('Alex')
    if (!userAlex) {
      userAlex = new User(username: 'Alex', pass: md5pass, passwd: md5pass, enabled: true, profile: new Profile(firstName: 'Alexander', lastName: 'Hoischen', clubFunction: ClubFunction.President)).save()
      new EmailAddress(email: 'alex@motsangria.de', isMain: true, user: userAlex).save()
      roleUser.addToPeople(userAlex )
    }

    def userAnne = User.findByUsername('Anne')
    if (!userAnne) {
      userAnne = new User(username: 'Anne', pass: md5pass, passwd: md5pass, enabled: true, profile: new Profile(firstName: 'Anne', lastName: 'Schulze', clubFunction: ClubFunction.Member))
      userAnne.save()
      new EmailAddress(email: 'anne@motsangria.de', isMain: true, user: userAnne).save()
      roleUser.addToPeople(userAnne)
    }

    def userBene = User.findByUsername('Bene')
    if (!userBene) {
      userBene = new User(username: 'Bene', pass: md5pass, passwd: md5pass, enabled: true, profile: new Profile(firstName: 'Benedikt', lastName: 'Hoheisel', clubFunction: ClubFunction.Member))
      userBene.save()
      new EmailAddress(email: 'bene@motsangria.de', isMain: true, user: userBene).save()
      roleUser.addToPeople(userBene)
    }

    def userCarsten = User.findByUsername('Carsten')
    if (!userCarsten) {
      userCarsten = new User(username: 'Carsten', pass: md5pass, passwd: md5pass, enabled: true, profile: new Profile(firstName: 'Carsten', lastName: 'Block', jabberId: 'carsten.block@gmail.com'))
      userCarsten.save()
      new EmailAddress(email: 'carsten@motsangria.de', isMain: true, user: userCarsten).save()
      roleUser.addToPeople(userCarsten)
      roleAdmin.addToPeople(userCarsten)
    }

    def userChristian = User.findByUsername('Christian')
    if (!userChristian) {
      userChristian = new User(username: 'Christian', pass: md5pass, passwd: md5pass, enabled: true, profile: new Profile(firstName: 'Christian', lastName: 'Weykamp'))
      userChristian.save()
      new EmailAddress(email: 'christian@motsangria.de', isMain: true, user: userChristian).save()
      roleUser.addToPeople(userChristian)
    }

    def userHappi = User.findByUsername('ChristianH')
    if (!userHappi) {
      userHappi = new User(username: 'ChristianH', pass: md5pass, passwd: md5pass, enabled: true, profile: new Profile(firstName: 'Christian', lastName: 'Happel', jabberId: 'christian.happel@gmail.com', clubFunction: ClubFunction.Reporter))
      userHappi.save()
      new EmailAddress(email: 'happel@motsangria.de', isMain: true, user: userHappi).save()
      roleUser.addToPeople(userHappi)
      roleAdmin.addToPeople(userHappi)
    }

    def userJan = User.findByUsername('Jan')
    if (!userJan) {
      userJan = new User(username: 'Jan', pass: md5pass, passwd: md5pass, enabled: true, profile: new Profile(firstName: 'Jan', lastName: 'Hay'))
      userJan.save()
      new EmailAddress(email: 'jan@motsangria.de', isMain: true, user: userJan).save()
      roleUser.addToPeople(userJan)
    }

    def userSebastian = User.findByUsername('Kochi')
    if (!userSebastian) {
      userSebastian = new User(username: 'Kochi', pass: md5pass, passwd: md5pass, enabled: true, profile: new Profile(firstName: 'Sebastian', lastName: 'Koch'))
      userSebastian.save()
      new EmailAddress(email: 'sebastian@motsangria.de', isMain: true, user: userSebastian).save()
      roleUser.addToPeople(userSebastian)
    }

    def userMartin = User.findByUsername('Martin')
    if (!userMartin) {
      userMartin = new User(username: 'Martin', pass: md5pass, passwd: md5pass, enabled: true, profile: new Profile(firstName: 'Martin', lastName: 'Niermann'))
      userMartin.save()
      new EmailAddress(email: 'martin@motsangria.de', isMain: true, user: userMartin).save()
      roleUser.addToPeople(userMartin)
    }

    def userPatrick = User.findByUsername('Patti')
    if (!userPatrick) {
      userPatrick = new User(username: 'Patti', pass: md5pass, passwd: md5pass, enabled: true, profile: new Profile(firstName: 'Patrick', lastName: 'Weller'))
      userPatrick.save()
      new EmailAddress(email: 'patrick@motsangria.de', isMain: true, user: userPatrick).save()
      roleUser.addToPeople(userPatrick)
    }

    def userPeter = User.findByUsername('Peter')
    if (!userPeter) {
      userPeter = new User(username: 'Peter', pass: md5pass, passwd: md5pass, enabled: true, profile: new Profile(firstName: 'Peter', lastName: 'Hoffmann'))
      userPeter.save()
      new EmailAddress(email: 'peter@motsangria.de', isMain: true, user: userPeter).save()
      roleUser.addToPeople(userPeter)
    }

    def userSam = User.findByUsername('Sam')
    if (!userSam) {
      userSam = new User(username: 'Sam', pass: md5pass, passwd: md5pass, enabled: true, profile: new Profile(firstName: 'Sam', lastName: 'Atkins'))
      userSam.save()
      new EmailAddress(email: 'sam@motsangria.de', isMain: true, user: userSam).save()
      roleUser.addToPeople(userSam)
    }

    def userStefan = User.findByUsername('Stefan')
    if (!userStefan) {
      userStefan = new User(username: 'Stefan', pass: md5pass, passwd: md5pass, enabled: true, profile: new Profile(firstName: 'Stefan', lastName: 'Hofmann'))
      userStefan.save()
      new EmailAddress(email: 'stefan@motsangria.de', isMain: true, user: userStefan).save()
      roleUser.addToPeople(userStefan)
    }

    def userTommy = User.findByUsername('Tommy')
    if (!userTommy) {
      userTommy = new User(username: 'Tommy', pass: md5pass, passwd: md5pass, enabled: true, profile: new Profile(firstName: 'Thomas', lastName: 'Preutenborbeck'))
      userTommy.save()
      new EmailAddress(email: 'tommy@motsangria.de', isMain: true, user: userTommy).save()
      roleUser.addToPeople(userTommy)
    }
  }
  def destroy = {
  }
} 